<?php
/**
 * 用户信息
 */

namespace app\admin\controller;

use app\common\model\Notice;
use app\common\model\Strategy;
use app\common\model\WxUsers;

class User extends Base
{
    /**
     * 用户列表
     */
    public function index()
    {
        $nickname = input('request.nickname', '', 'trim');
        $page = input('request.page', 1, 'intval');
        $size = input('size', 10);
        $where = array();
        if (!empty($nickname)) {
            $where['nickname'] = array('like', '%' . $nickname . '%');
        }
        $list = WxUsers::where($where)->order('id desc')->page($page, $size)->select();
        $total = WxUsers::where($where)->count();
        $this->assign('list', $list);
        $this->assign('total', $total);
        $this->assign('nowposition', '用户列表');
        return $this->fetch();
    }

    /**
     *  攻略管理
     */
    public function strategy()
    {
        $detail = Strategy::order('id', 'desc')->find();
        $this->assign('detail', $detail);
        $this->assign('nowposition', '攻略管理');
        return $this->fetch();
    }

    /**
     * 更新攻略信息
     */
    public function updateStrategy()
    {
        $id = input('id');
        $thum = input('thum');
        $image = input('image');
        if ($id) {
            Strategy::where(['id' => $id])->update(['thum' => $thum, 'image' => $image, 'add_time' => date('Y-m-d H:i:s')]);
            return json(rm(1, '更新成功', ''));
        }
        return json(rm(0, '信息不存在', ''));
    }

    /**
     * 获取公告列表
     */
    public function notice()
    {
        $page = input('request.page', 1, 'intval');
        $size = input('size', 10);
        $list = Notice::order('id desc')
            ->page($page, $size)
            ->select();
        $total = Notice::count();
        $this->assign('list', $list);
        $this->assign('total', $total);
        $this->assign('nowposition', '公告列表');
        return $this->fetch();
    }

    /**
     * 获取公告详细内容
     */
    public function noticeDetail()
    {
        $id = input('id');
        if ($id) {
            $detail = Notice::get($id);
            return json(rm(1, '获取成功', $detail));
        }
        return json(rm(0, '信息不存在'));
    }

    /**
     * 更新公告
     */
    public function updateNotice()
    {
        $id = input('id');
        $data['title'] = input('title', '');
        $data['image'] = input('image', '');
        $data['content'] = input('content', '');
        if ($id) {
            Notice::where(['id' => $id])->update($data);
            return json(rm(1, '更新成功', ''));
        } else {
            Notice::insert($data);
            return json(rm(1, '新增成功', ''));
        }
    }

    /**
     *  删除公告
     */
    public function delNotice()
    {
        $id = input('id');
        if ($id) {
            Notice::where(['id' => $id])->delete();
            return json(rm(1, '删除成功', ''));
        }
        return json(rm(0, '信息不存在', ''));
    }
}